<?php

namespace App\Console\Commands;

use App\Models\Device;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class SyncDevice extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'syncDevice';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'redis 同步到mysql';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $redis = app("redis");
        $start = time();
        Device::chunk(10000 , function ($devices) use ($redis){
            $redis->pipeline(function ($pipe) use ($devices){
                foreach ($devices as $code){
                    $key = "device:list:{$code -> status}:{$code -> tp}" ;
                    $pipe -> lpush($key, $code -> sn);
                    $pipe->hmset("msg:device:{$code->sn}",$code -> toArray());
                }
            });
        });
        $this -> info(time() - $start);
    }
}
